package cn.com.twoke.application.game.mario.core.ecs.component.render.group;

import cn.com.twoke.application.game.mario.core.ecs.component.render.RenderComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.render.RenderComponentHandler;
import cn.com.twoke.application.game.mario.core.ecs.entity.Entity;
import cn.com.twoke.application.game.mario.core.ecs.system.RenderSystem;
import cn.hutool.core.collection.CollUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class GroupRenderComponentHandler implements RenderComponentHandler<GroupRenderComponent> {


    @Override
    public void handle(Entity entity, BufferedImage container, Graphics2D g, GroupRenderComponent component) {
        List<RenderComponent> components = component.getComponents()
                .stream().sorted()
                .filter(RenderComponent::isDisplay)
                .collect(Collectors.toList());
        if (CollUtil.isNotEmpty(components)) {
            for (RenderComponent renderComponent : components) {
                RenderComponentHandler renderComponentHandler = RenderSystem.getRenderComponentHandler(renderComponent);
                if (Objects.nonNull(renderComponentHandler)) {
                    renderComponentHandler.handle(entity, container, g, renderComponent);
                }
            }
        }
    }

}
